<h1><?php echo $this->pageTitle = __('Daily Statistics', true); ?></h1>
<div class="index">
	<?php 
		echo $javascript->link('/lilads/js/datepicker', false);
		echo $html->css('/lilads/css/datepicker', false);
		
		if (empty($params['banner_id'])) {
			echo '<h2>'.h($campaign['Campaign']['title']).'</h2>';
			foreach ($campaign['Banner'] as $banner) {
				echo '<div style="margin-left: 20px">'.$html->link('#'.$banner['id'].' '.$banner['title'], am($params, array('banner_id'=>$banner['id']))).'</div>';
			}
		} else {
			echo '<div style="margin-left: 12px">'.$html->link($campaign['Campaign']['title'], am($params, array('banner_id'=>null))).'</div>';
			foreach ($campaign['Banner'] as $banner) {
				if ($banner['id'] == $params['banner_id']) {
					echo '<h2>#'.$banner['id'].' '.h($banner['title']).'</h2>';
				} else {
					echo '<div style="margin-left: 20px">'.$html->link('#'.$banner['id'].' '.$banner['title'], am($params, array('banner_id'=>$banner['id']))).'</div>';
				}
			}
		}
	?>
	<br />
	<script type="text/javascript">
		function SetDatePopup(link, id) {
			if (!id) id = 'available';
			var picked = datePickerController.datePickers[id].date;

			var d  = (picked.getDate() < 10) ? "0" + picked.getDate() : picked.getDate();
			var m  = ((picked.getMonth() + 1) < 10) ? "0" + (picked.getMonth() + 1) : picked.getMonth() + 1;
			document.location.href = link.replace(/\%d/, picked.getFullYear()+'-'+m+'-'+d);
		};
	</script>
	<div class="filter"> 
		<?php 
			__('Dates from'); echo ' ';
			echo $html->link(strftime(Configure::read('Lilads.dateShort'), strtotime($params['start'])), 'javascript:return void(0);', array('id'=>'fd-but-start'));
			echo ' '.__('to', true).' ';
			echo $html->link(strftime(Configure::read('Lilads.dateShort'), strtotime($params['end'])), 'javascript:return void(0);', array('id'=>'fd-but-end')); 
		?>.
	</div>
	<?php
		echo '<input type="hidden" name="start" id="start" value="'.h($params['start']).'" class="format-y-m-d divider-dash no-transparency" onchange="SetDatePopup(\''.$html->url(am($params, array('start'=>'%d'))).'\', \'start\');" />';
		echo '<input type="hidden" name="end" id="end" value="'.h($params['end']).'" class="format-y-m-d divider-dash no-transparency" onchange="SetDatePopup(\''.$html->url(am($params, array('end'=>'%d'))).'\', \'end\');" />';
	?>
	<table cellspacing="1" cellpadding="1">
		<tr>
			<th align="center"><?php __('Date'); ?></th>
			<th align="center"><?php __('Click Count'); ?></th>
			<th align="center"><?php __('Display Count'); ?></th>
			<th align="center"><?php __('CTR [%]'); ?></th>
		</tr>
		<?php
			$i = 0; $sumClicks = 0; $sumViews = 0;
						
			$start_day = strtotime($params['start']);
			$end_day = strtotime($params['end']);
			 
			reset($stats); $st = current($stats);
			for ($i=$start_day; $i<=$end_day; $i+=24*60*60) {
				if (isset($st[0]['date_hour']) && strftime('%Y-%m-%d', strtotime($st[0]['date_hour']))==strftime('%Y-%m-%d', $i)) {
					$clicks = $st[0]['clicks'];
					$views = $st[0]['views'];
					$ctr = sprintf('%.2f', ($st[0]['views']==0)?0:$st[0]['clicks']/$st[0]['views']*100);
					$sumClicks += $st[0]['clicks'];
					$sumViews += $st[0]['views'];
					$st = next($stats);
				} else {
					$clicks = 0;
					$views = 0;
					$ctr = 0;
				}
		?>
		<tr<?php echo (((int)($i/(24*60*60)) % 2 == 0)?' class="altrow"':''); ?>>
			<td align="center"><?php echo $html->link(strftime(Configure::read('Lilads.dateShort'), $i), am(array_intersect_key($params, array('campaign_id'=>'', 'banner_id'=>'')), array('action'=>'admin_hourly', 'date'=>strftime('%Y-%m-%d')))); ?></td>
			<td align="center"><?php echo $clicks; ?></td>
			<td align="center"><?php echo $views; ?></td>
			<td align="center"><?php echo $ctr; ?></td>
		</tr>
		<?php
			}
		?>
		<tr>
			<th align="center">SUM </th>
			<th align="center"><?php echo $sumClicks; ?></th>
			<th align="center"><?php echo $sumViews; ?></th>
			<th align="center"><?php printf('%.2f', ($sumViews==0)?0:$sumClicks/$sumViews*100); ?></th>
		</tr>
	</table>
</div>
